# -*- coding: utf8 -*-
from soccerbots.core.structs import *

HEIGHT = 1800
WIDTH = 2500

ini_atacant = {}
ini_atacant['goalkeeper'] = [(-1000, HEIGHT/2), 270]
ini_atacant['player1'] = [(60, HEIGHT/2), 180]
ini_atacant['player2'] = [(-200, (HEIGHT/2)+150), 340]
ini_atacant['player3'] = [(-200, (HEIGHT/2)-150), 20]
ini_atacant['player4'] = [(-750, HEIGHT/2), 0]

ini_defensa = {}
ini_defensa['goalkeeper'] = [(1000, HEIGHT/2), 90]
ini_defensa['player1'] = [(750, HEIGHT/2), 180]
ini_defensa['player2'] = [(300, (HEIGHT/2)+100), 190]
ini_defensa['player3'] = [(300, (HEIGHT/2)-100), 170]
ini_defensa['player4'] = [(300, HEIGHT/2), 180]

penalty_atacant = {}
penalty_atacant['goalkeeper'] = [(-1000, HEIGHT/2), 270]
penalty_atacant['player1'] = [(-50, (HEIGHT/2)), 0]
penalty_atacant['player2'] = [(-50, (HEIGHT/2)+200), 0]
penalty_atacant['player3'] = [(-50, (HEIGHT/2)-200), 0]
penalty_atacant['player4'] = [(590, (HEIGHT/2)), 0]

penalty_defensa = {}
penalty_defensa['goalkeeper'] = [(1000, HEIGHT/2), 90]
penalty_defensa['player1'] = [(-50, (HEIGHT/2)+100), 0]
penalty_defensa['player2'] = [(-50, (HEIGHT/2)+300), 0]
penalty_defensa['player3'] = [(-50, (HEIGHT/2)-100), 0]
penalty_defensa['player4'] = [(-50, (HEIGHT/2)-300), 0]

penalty_ball = (710, HEIGHT/2)

freekick_atacant = {}
freekick_atacant['goalkeeper'] = [(-1000, HEIGHT/2), 270]
freekick_atacant['player1'] = [(400, (HEIGHT/2)+(HEIGHT/4)), 0]
freekick_atacant['player2'] = [(400, (HEIGHT/2)-(HEIGHT/4)), 0]
freekick_atacant['player3'] = [(-590, (HEIGHT/2)), 0]
freekick_atacant['player4'] = [(590, (HEIGHT/2)), 0]

freekick_defensa = {}
freekick_defensa['goalkeeper'] = [(1000, HEIGHT/2), 90]
freekick_defensa['player1'] = [(850, (HEIGHT/2)+150), 0]
freekick_defensa['player2'] = [(850, (HEIGHT/2)-150), 0]
freekick_defensa['player3'] = [(770, (HEIGHT/2)+320), 0]
freekick_defensa['player4'] = [(770, (HEIGHT/2)-320), 0]

a_freeball_ball = (550,1500)
a_freeball_a = {}
a_freeball_a['goalkeeper'] = [(1000, (HEIGHT/2)+150), 90]
a_freeball_a['player1'] = [(800, 1500), 180]
a_freeball_a['player2'] = [(200, (HEIGHT/2)-100), 0]
a_freeball_a['player3'] = [(500, (HEIGHT/2)-100), 0]
a_freeball_a['player4'] = [(900, (HEIGHT/2)-100), 0]
a_freeball_b = {}
a_freeball_b['goalkeeper'] = [(-1000, (HEIGHT/2)), 270]
a_freeball_b['player1'] = [(300, 1500), 0]
a_freeball_b['player2'] = [(100, (HEIGHT/2)-100), 0]
a_freeball_b['player3'] = [(-100, (HEIGHT/2)+500), 0]
a_freeball_b['player4'] = [(-100, (HEIGHT/2)+350), 0]

b_freeball_ball = (-550,1500)
b_freeball_a = {}
b_freeball_a['goalkeeper'] = [(-1000, (HEIGHT/2)+150), 270]
b_freeball_a['player1'] = [(-800, 1500), 0]
b_freeball_a['player2'] = [(-200, (HEIGHT/2)-100), 180]
b_freeball_a['player3'] = [(-500, (HEIGHT/2)-100), 180]
b_freeball_a['player4'] = [(-900, (HEIGHT/2)-100), 180]
b_freeball_b = {}
b_freeball_b['goalkeeper'] = [(1000, (HEIGHT/2)), 90]
b_freeball_b['player1'] = [(-300, 1500), 180]
b_freeball_b['player2'] = [(-100, (HEIGHT/2)-100), 180]
b_freeball_b['player3'] = [(100, (HEIGHT/2)+500), 180]
b_freeball_b['player4'] = [(100, (HEIGHT/2)+350), 180]

c_freeball_ball = (-550,300)
c_freeball_a = {}
c_freeball_a['goalkeeper'] = [(-1000, (HEIGHT/2)-150), 270]
c_freeball_a['player1'] = [(-800, 300), 0]
c_freeball_a['player2'] = [(-200, (HEIGHT/2)+100), 180]
c_freeball_a['player3'] = [(-500, (HEIGHT/2)+100), 180]
c_freeball_a['player4'] = [(-900, (HEIGHT/2)+100), 180]
c_freeball_b = {}
c_freeball_b['goalkeeper'] = [(1000, (HEIGHT/2)), 90]
c_freeball_b['player1'] = [(-300, 300), 180]
c_freeball_b['player2'] = [(-100, (HEIGHT/2)+100), 180]
c_freeball_b['player3'] = [(100, (HEIGHT/2)-500), 180]
c_freeball_b['player4'] = [(100, (HEIGHT/2)-350), 180]

d_freeball_ball = (550,300)
d_freeball_a = {}
d_freeball_a['goalkeeper'] = [(1000, (HEIGHT/2)-150), 90]
d_freeball_a['player1'] = [(800, 300), 180]
d_freeball_a['player2'] = [(200, (HEIGHT/2)+100), 0]
d_freeball_a['player3'] = [(500, (HEIGHT/2)+100), 0]
d_freeball_a['player4'] = [(900, (HEIGHT/2)+100), 0]
d_freeball_b = {}
d_freeball_b['goalkeeper'] = [(-1000, (HEIGHT/2)), 270]
d_freeball_b['player1'] = [(300, 300), 0]
d_freeball_b['player2'] = [(100, (HEIGHT/2)+100), 0]
d_freeball_b['player3'] = [(-100, (HEIGHT/2)-500), 0]
d_freeball_b['player4'] = [(-100, (HEIGHT/2)-350), 0]

def simetric_pos(pos):
    return (pos[0]*-1, pos[1])

def simetric(team):
    nteam = {}
    for k,v in team.iteritems():
        pos = v[0]
        dir = v[1]
        nteam[k] = [(pos[0]*-1,pos[1]), (dir+180)%360]
    return nteam

def vsimetric_pos(pos):
    return (pos[0],1800-pos[1])

def vsimetric(team):
    nteam = {}
    for k,v in team.iteritems():
        pos = v[0]
        dir = v[1]
        nteam[k] = [(pos[0], 1800-pos[1]), (dir+180)%360]
    return nteam

def freeballState(quadrant=1, part=1):
    state = GameState()

    if quadrant==1:
        state.ball.position = a_freeball_ball
        teams = [a_freeball_a,a_freeball_b]
        nteams = [state.teamB, state.teamA]
    elif quadrant==2:
        state.ball.position = b_freeball_ball
        teams = [b_freeball_b,b_freeball_a]
        nteams = [state.teamB, state.teamA]
    elif quadrant==3:
        state.ball.position = c_freeball_ball
        teams = [c_freeball_b,c_freeball_a]
        nteams = [state.teamB, state.teamA]
    elif quadrant==4:
        state.ball.position = d_freeball_ball
        teams = [d_freeball_a,d_freeball_b]
        nteams = [state.teamB, state.teamA]
    else:
        print 'Invalid atacant value'
        raise RuntimeError
 
    if part==2: nteams=[nteams[1],nteams[0]]

    for i in range(2):
        for k in TeamState.keyset:
            nteams[i][k].position = teams[i][k][0]
            nteams[i][k].direction = teams[i][k][1]
    return state

def freekickState(atacant=1, part=1):
    state = GameState()
 
    if atacant==1:
        teams = [freekick_atacant,freekick_defensa]
        state.ball.position = penalty_ball
    elif atacant==2:
        teams = [simetric(freekick_defensa),simetric(freekick_atacant)]
        state.ball.position = simetric_pos(penalty_ball)
    else:
        print 'Invalid atacant value'
        raise RuntimeError

    if part==2:
        teams = [teams[1],teams[0]]

    nteams = [state.teamA, state.teamB]
    for i in range(2):
        for k in TeamState.keyset:
            nteams[i][k].position = teams[i][k][0]
            nteams[i][k].direction = teams[i][k][1]

    return state

def penaltyState(atacant=1, part=1):
    state = GameState()
 
    if atacant==1:
        teams = [penalty_atacant,penalty_defensa]
        state.ball.position = penalty_ball
    elif atacant==2:
        teams = [simetric(penalty_defensa),simetric(penalty_atacant)]
        state.ball.position = simetric_pos(penalty_ball)
    else:
        print 'Invalid atacant value'
        raise RuntimeError

    if part==2:
        teams = [teams[1],teams[0]]

    nteams = [state.teamA, state.teamB]
    for i in range(2):
        for k in TeamState.keyset:
            nteams[i][k].position = teams[i][k][0]
            nteams[i][k].direction = teams[i][k][1]

    return state

def iniState(atacant=1, part=1):
    state = GameState()
    state.ball.position = (0,900)

    if atacant==1:
        teams = [ini_atacant,ini_defensa]
    elif atacant==2:
        teams = [ini_defensa,ini_atacant]
    else:
        print 'Invalid atacant value'
        raise RuntimeError

    if part==1:
        pass
    elif part==2:
        teams = [simetric(teams[0]), simetric(teams[1])]
    else:
        print 'Invalid atacant value'
        raise RuntimeError

    nteams = [state.teamA, state.teamB]
    for i in range(2):
        for k in TeamState.keyset:
            nteams[i][k].position = teams[i][k][0]
            nteams[i][k].direction = teams[i][k][1]

    return state
